package com.example.dell.fastshopping.order;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.Trade.TradeRechargeActivity;
import com.example.dell.fastshopping.order.model.OrderViewModel;
import com.example.dell.fastshopping.utils.DateUtils;
import com.example.dell.fastshopping.utils.GlideUtils;
import com.example.dell.fastshopping.views.BaseListFragmnet;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class OrderFragment extends BaseListFragmnet {

    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.nodata)
    LinearLayout nodata;
    @BindView(R.id.refresh)
    TwinklingRefreshLayout refresh;
    private OrderViewModel mViewModel;
    private Unbinder bind;
    private List<OrderInfo> mList;
    private Context mContext;
    private int status;

    public static OrderFragment newInstant(Integer i) {
        OrderFragment orderFragment = new OrderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("status", i);
        orderFragment.setArguments(bundle);
        return orderFragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order, container, false);
        bind = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(OrderViewModel.class);
        initView();
    }


    @Override
    protected TwinklingRefreshLayout getRefreshLayout() {
        return refresh;
    }

    private void initView() {
        status = getArguments().getInt("status", -1);
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        // rv.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
    }

    @Override
    public void onResume() {
        super.onResume();
        pageNum = 1;
        pageSize = 10;
        getNetData(1, 10);
    }

    @Override
    protected void getNetData(final int page, int pageSize) {
        mViewModel.getOrderList(page, pageSize, status).observe(this, new Observer<List<OrderInfo>>() {
            @Override
            public void onChanged(@Nullable List<OrderInfo> list) {
                if (page == 1) {
                    mList = list;
                } else {
                    mList.addAll(list);
                }
                if (mList == null || mList.size() == 0) {
                    nodata.setVisibility(View.VISIBLE);
                } else {
                    nodata.setVisibility(View.GONE);
                }
                loadFinsh();
                RecyclerView.Adapter adapter = rv.getAdapter();
                if (adapter == null) {
                    rv.setAdapter(new MyAdapter());
                } else {
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        bind.unbind();
    }

    private class MyAdapter extends RecyclerView.Adapter<MyHolder> {

        @NonNull
        @Override
        public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_order, viewGroup, false);
            return new MyHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
            OrderInfo orderInfo = mList.get(i);
            myHolder.orderName.setText(orderInfo.getProductName());
            myHolder.orderNumber.setText(orderInfo.getOrderId());
            myHolder.orderStatus.setText(orderInfo.getStatusString());
            GlideUtils.setView(mContext, orderInfo.getPicString(), myHolder.orderIcon);
            myHolder.outTime.setText(DateUtils.stampToDate(orderInfo.getOutTime()));
            myHolder.total_price.setText(orderInfo.getTotalPriceString());
            myHolder.itemView.setOnClickListener(myHolder);
            myHolder.deal_with.setOnClickListener(myHolder);
            myHolder.setOrderId(orderInfo.getId());
            myHolder.setPosition(i);

            if (orderInfo.getStatus() == 2) {
                myHolder.button_contain.setVisibility(View.VISIBLE);
            } else {
                myHolder.button_contain.setVisibility(View.GONE);
            }
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }
    }

    private class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {


        public TextView orderName;
        public TextView orderStatus;
        public ImageView orderIcon;
        public TextView orderNumber;
        public TextView outTime;
        public TextView total_price;
        public TextView deal_with;
        public TextView cancle_order;
        private int orderId;
        public RelativeLayout button_contain;
        private int position;

        public MyHolder(@NonNull View itemView) {
            super(itemView);

            orderNumber = (TextView) itemView.findViewById(R.id.order_number);
            orderStatus = itemView.findViewById(R.id.order_status);
            orderIcon = itemView.findViewById(R.id.order_icon);
            orderName = itemView.findViewById(R.id.order_name);
            outTime = itemView.findViewById(R.id.out_time);
            total_price = itemView.findViewById(R.id.total_price);
            deal_with = itemView.findViewById(R.id.deal_with);
            cancle_order = itemView.findViewById(R.id.cancle_order);
            button_contain = itemView.findViewById(R.id.button_contain);

        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.item:
                    toDetialPage(orderId);
                    break;
                case R.id.deal_with:
                    toPayPage(mList.get(position));
                    break;
                case R.id.cancle_order:
                    //TODO:
                    break;
            }
        }


        public void setOrderId(int id) {
            this.orderId = id;
        }

        public void setPosition(int i) {
            this.position = i;
        }
    }

    private void toPayPage(OrderInfo mOrderInfo) {
        Intent intent = new Intent(mContext, TradeRechargeActivity.class);
        intent.putExtra("from", "order");
        intent.putExtra("orderInfo", mOrderInfo);
        startActivityForResult(intent, 104);
    }

    private void toDetialPage(int orderId) {
        Intent intent = new Intent(mContext, OrderInfoActivity.class);
        intent.putExtra("id", orderId);
        startActivity(intent);
    }


}
